package cz.proks.simplecalendarmanager.server.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import cz.proks.simplecalendarmanager.server.Constants;
import cz.proks.simplecalendarmanager.server.utils.ServletUtils;
import cz.proks.simplecalendarmanager.shared.model.AccessToken;

public abstract class SCMRemoteServiceServlet extends RemoteServiceServlet {
	protected final HttpSession getSession() {
		return getRequest().getSession();
	}
	
	protected final HttpServletRequest getRequest() {
		return getThreadLocalRequest();
	}
	
	protected final HttpServletResponse getResponse() {
		return getThreadLocalResponse();
	}
	
	protected final void setToSession(String key, Object obj) {
		getSession().setAttribute(key, obj);
	}
	
	protected final void removeFromSession(String key) {
		getSession().removeAttribute(key);
	}
	
	protected final <T> T getFromSession(String key) {
		return (T) getSession().getAttribute(key);
	}
	
	protected final AccessToken getAccessTokenFromSession(String accessTokenScope) {
		return ServletUtils.getAccessTokenFromSession(accessTokenScope, getSession());
	}
	
	protected final void setAccessTokenToSession(String accessTokenScope, AccessToken accessToken) {
		ServletUtils.setAccessTokenToSession(accessTokenScope, accessToken, getSession());		
	}
	
	protected final void removeAccessTokenFromSession(String scope) {
		ServletUtils.removeAccessTokenFromSession(scope, getSession());
	}
}
